package jp.lab1;
import java.util.Vector;


public class SecureNodeList extends Vector <SecureNode> {
	
	public SecureNodeList(){
		
	}
	
	public SecureNode rekey(String secretID){
		SecureNode node = null;
		int nLists = size();
		//System.out.println("Secure Node list size="+nLists);
		
		for(int n=0; n<nLists; n++){
			node = getNode(n);
			String nodegroupkeyID = node.getgroupkeyID();
			//System.out.println("Node id="+node.getName());
			//System.out.println("Node group key id="+nodegroupkeyID);
			
			if(node.isRegistered()){ //check only registed node
				if(!nodegroupkeyID.equals(secretID)){
					//System.out.println("Rekey not success yet");
					return node;
				}
			}
			
		}
		//System.out.println("Rekey successed");
		return null;
		
	}
	
	public boolean removeNode(String name){
		SecureNode node = null;
		int nLists = size();
		
		for(int n=0; n<nLists; n++){
			node = getNode(n);
			String nodeName = node.getName();
			
			if(nodeName.equals(name)){
				remove(n);
				return true;
			}
		}
		return false;
	}
	
	public void printNodeInfo(){
		SecureNode node = null;
		int nLists = size();
		
		for(int n=0; n<nLists; n++){
			node = getNode(n);
			if(node.isRegistered()){
				System.out.println("Node id="+node.getName()+" groupkey id="+node.getgroupkeyID());
			}else{
				System.out.println("Node id="+node.getName()+ " *** not registered ***");
			}
		}
	}
	
	private SecureNode getNode(int n){
		return (SecureNode)get(n);
	}
	
	public SecureNode getSecureNode(String name){
		if(name == null)
			return null;
		
		int nLists = size();
		for(int n=0; n<nLists; n++){
			SecureNode node = getNode(n);
			String nodeName = node.getName();
			
			if(name.compareTo(nodeName)== 0){
				return node;
			}
		}
		return null;
	}
	
	public int getRegisteredNode(){
		int nodeNum = 0;
		for(int i=0; i<size(); i++){
			SecureNode node = getNode(i);
			if(node.isRegistered()){
				nodeNum ++;
			}
		}
		
		return nodeNum;
	}


}
